package com.gk.javaservice.util.time;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

//时间对比，计算，工具类
public class TimeUtil {

    public static void main(String[] args) {


        // dateFormat();
        // // System.out.println(getDayBetweenToWeek("2020-06-20 09:30:55", "2020-06-14 09:20:55"));
//        dateFormatForMun();


    }

    //返回前一天
    public String beforeday(String day){
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern("yyyy-MM-dd");

        String dd="";
        try {

            Date date = sdf.parse(day);
            Date newDate = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_MONTH, -1); //当前时间减去一天，即一天前的时间
            String beforeday = dateFormat.format(calendar.getTime());
            //calendar.add(Calendar.YEAR, 1);//增加一年

//calendar.add(Calendar.DATE, 1);//增加一天

//calendar.add(Calendar.DATE, -10);//减10天

//calendar.add(Calendar.MONTH, 1);//增加一个月
            // System.out.println("beforeday:" + beforeday);
            dd=beforeday;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dd;
    }
    //时间+1天
    public static String dayplusone(String day){
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern("yyyy-MM-dd");

        String dd="";
        try {

            Date date = sdf.parse(day);
            Date newDate = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_MONTH, 1); //当前时间减去一天，即一天前的时间
            String beforeday = dateFormat.format(calendar.getTime());
            //calendar.add(Calendar.YEAR, 1);//增加一年

//calendar.add(Calendar.DATE, 1);//增加一天

//calendar.add(Calendar.DATE, -10);//减10天

//calendar.add(Calendar.MONTH, 1);//增加一个月
//            // System.out.println("dayplusone:" + beforeday);
            dd=beforeday;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dd;
    }
    //获取当前时间并格式化
    public static String dateFormat() {
        Date date = new Date();
        String str = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(str);
        // System.out.println(sdf.format(date));
        return sdf.format(date);
    }

    public static String dateFormatDay() {
        Date date = new Date();
        String str = "yyyy-MM-dd";
        SimpleDateFormat sdf = new SimpleDateFormat(str);
        // System.out.println(sdf.format(date));
        return sdf.format(date);
    }

    public static String dateFormatForMun() {
        Date date = new Date();
        String str = "yyyyMMddHHmmss";
        SimpleDateFormat sdf = new SimpleDateFormat(str);
        // System.out.println(sdf.format(date));
        return sdf.format(date);
    }

    //时间格式化
    public static String dateFormatForMun2() {
        Date date = new Date();
        String str = "yyyyMMdd";
        SimpleDateFormat sdf = new SimpleDateFormat(str);
        // System.out.println(sdf.format(date));
        return sdf.format(date);
    }

    /**
     * 获取两个相差的天数
     * 开始时间比结束时间小的话会返回负数的差值
     *
     * @param startDateStr
     * @param endDateStr
     */
    public static int getDayBetweenTwoDate(String startDateStr, String endDateStr) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int days = 0;
        try {
            Date startDate = simpleDateFormat.parse(startDateStr);
            Date endDate = simpleDateFormat.parse(endDateStr);
            Calendar startCalendar = Calendar.getInstance();
            startCalendar.setTime(startDate);
            Calendar endCalendar = Calendar.getInstance();
            endCalendar.setTime(endDate);
            //计算两个日期相差的天数
            //startCalendar.getTime().getTime()返回long毫秒数形式,毫秒转为秒所以除以1000
            //1天=24小时，1小时=60分，1分=60秒，所以两个时间的差再除以60 * 60 * 24换算成天的形式
            days = ((int) (startCalendar.getTime().getTime() / 1000) - (int) (endCalendar.getTime().getTime() / 1000)) / (60 * 60 * 24);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return days;
    }

    /**
     * date2比date1多的周数
     *
     * @param startDateStr
     * @param endDateStr
     * @return
     */
    public static int getDayBetweenToWeek(String startDateStr, String endDateStr) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int weekOffset = 0;
        try {
            Date startDate = simpleDateFormat.parse(startDateStr);
            Date endDate = simpleDateFormat.parse(endDateStr);
            Calendar cal = Calendar.getInstance();
            cal.setTime(startDate);
            int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
            dayOfWeek = dayOfWeek - 1;
            if (dayOfWeek == 0) dayOfWeek = 7;

            int dayOffset = getDayBetweenTwoDate(startDateStr, endDateStr);

            weekOffset = dayOffset / 7;
            int a;
            if (dayOffset > 0) {
                a = (dayOffset % 7 + dayOfWeek > 7) ? 1 : 0;
            } else {
                a = (dayOfWeek + dayOffset % 7 < 1) ? -1 : 0;
            }
            weekOffset = weekOffset + a;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return weekOffset;
    }


    /**
     * 获取两个日期的月数差
     *
     * @param fromDate
     * @param toDate
     * @return
     */
    public static long getDifferMonth(Date fromDate, Date toDate) {
        Calendar fromDateCal = Calendar.getInstance();
        Calendar toDateCal = Calendar.getInstance();
        fromDateCal.setTime(fromDate);
        toDateCal.setTime(toDate);

        int fromYear = fromDateCal.get(Calendar.YEAR);
        int toYear = toDateCal.get((Calendar.YEAR));
        if (fromYear == toYear) {
            return Math.abs(fromDateCal.get(Calendar.MONTH) - toDateCal.get(Calendar.MONTH));
        } else {
            int fromMonth = 12 - (fromDateCal.get(Calendar.MONTH) + 1);
            int toMonth = toDateCal.get(Calendar.MONTH) + 1;
            return Math.abs(toYear - fromYear - 1) * 12 + fromMonth + toMonth;
        }
    }

    //时间大小差值-单位年

    /**
     * 获取两个日期相差的小时
     * 开始时间比结束时间小的话会返回负数的差值
     *
     * @param startDateStr
     * @param endDateStr
     */
    public static int getDayBetweenToHouse(String startDateStr, String endDateStr) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int days = 0;
        try {
            Date startDate = simpleDateFormat.parse(startDateStr);
            Date endDate = simpleDateFormat.parse(endDateStr);
            Calendar startCalendar = Calendar.getInstance();
            startCalendar.setTime(startDate);
            Calendar endCalendar = Calendar.getInstance();
            endCalendar.setTime(endDate);
            //计算两个日期相差的天数
            //startCalendar.getTime().getTime()返回long毫秒数形式,毫秒转为秒所以除以1000
            //1天=24小时，1小时=60分，1分=60秒，所以两个时间的差再除以60 * 60 * 24换算成天的形式
            days = ((int) (startCalendar.getTime().getTime() / 1000) - (int) (endCalendar.getTime().getTime() / 1000)) / (60 * 60);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return days;
    }

    /**
     * 获取两个日期相差的小时
     * 开始时间比结束时间小的话会返回负数的差值
     *
     * @param startDateStr
     * @param endDateStr
     */
    public static int getDayBetweenToMinutes(String startDateStr, String endDateStr) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int days = 0;
        try {
            Date startDate = simpleDateFormat.parse(startDateStr);
            Date endDate = simpleDateFormat.parse(endDateStr);
            Calendar startCalendar = Calendar.getInstance();
            startCalendar.setTime(startDate);
            Calendar endCalendar = Calendar.getInstance();
            endCalendar.setTime(endDate);
            //计算两个日期相差的天数
            //startCalendar.getTime().getTime()返回long毫秒数形式,毫秒转为秒所以除以1000
            //1天=24小时，1小时=60分，1分=60秒，所以两个时间的差再除以60 * 60 * 24换算成天的形式
            days = ((int) (startCalendar.getTime().getTime() / 1000) - (int) (endCalendar.getTime().getTime() / 1000)) / (60);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return days;
    }

    //时间大小差值-单位秒
    public static int getDayBetweenToSECOND(String startDateStr, String endDateStr) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int days = 0;
        try {
            Date startDate = simpleDateFormat.parse(startDateStr);
            Date endDate = simpleDateFormat.parse(endDateStr);
            Calendar startCalendar = Calendar.getInstance();
            startCalendar.setTime(startDate);
            Calendar endCalendar = Calendar.getInstance();
            endCalendar.setTime(endDate);
            //计算两个日期相差的天数
            //startCalendar.getTime().getTime()返回long毫秒数形式,毫秒转为秒所以除以1000
            //1天=24小时，1小时=60分，1分=60秒，所以两个时间的差再除以60 * 60 * 24换算成天的形式
            days = ((int) (startCalendar.getTime().getTime() / 1000) - (int) (endCalendar.getTime().getTime() / 1000));

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return days;
    }

    //时间大小对比
    public static boolean sizeComparison(String startDateStr, String endDateStr) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//创建日期转换对象HH:mm:ss为时分秒，年月日为yyyy-MM-dd
        boolean flag = false;
        try {
            Date dt1 = df.parse(startDateStr);//将字符串转换为date类型
            Date dt2 = df.parse(endDateStr);
            if (dt1.getTime() > dt2.getTime())//比较时间大小,如果dt1大于dt2
            {
                // // System.out.println("yes");
                flag = true;
            } else {
                // // System.out.println("no");//运行输出no
                flag = false;
            }
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return flag;

    }


    //时间大小对比2
    public static boolean sizeComparison2() {
        //DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//创建日期转换对象HH:mm:ss为时分秒，年月日为yyyy-MM-dd
        boolean flag = false;

        Date date = new Date();
        String str = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(str);
        // System.out.println(sdf.format(date));
        String str2 = "yyyyMMddHHmmss";
        SimpleDateFormat sdf2 = new SimpleDateFormat(str2);
        // System.out.println(sdf2.format(date));

        return flag;

    }


}
